<?php

namespace app\api\controller;

use app\api\model\AdjustScoreLog;
use app\api\model\Category;
use app\api\model\ExamLog;
use app\api\model\UserScoreLog;
use app\api\model\UserSign;
use app\common\controller\Api;
use app\api\model\User as UserModel;
use think\Config;
use app\api\controller\User as UserController;

class Manage extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function scoreRanking()
    {
        $UserModel = new UserModel();
        $Category = new Category();
        $UserController = new UserController();
        $list=$UserModel->order('all_score desc')->field('id,nickname,mobile,avatar,identity,all_score,jointime')->paginate()->each(function($item,$key)use($Category,$UserController){
            $item['miss_exam']=$UserController->MissExamCount($item['id'],date('Y-m-d',$item['jointime']),date('Y-m-d',time()));
            $item['pass']=$this->passUser($item['id']);//通过率
            $item['attendance']=$this->signUser($item['id'],$item['jointime']);//出勤率
            $item['identity']=$Category->where(['id'=>$item['identity']])->value('name');
            return $item;
        });
        $this->success('',$list);
    }
    public function passUser($user_id)
    {
        $ExamLog = new ExamLog();
        $where_all['user_id']=$user_id;
        $where_all['status']=1;
        $all_count=$ExamLog->where($where_all)->count('id');
        $where_pass['user_id']=$user_id;
        $where_pass['status']=1;
        $where_pass['score']=['>',60];
        $pass_count=$ExamLog->where($where_pass)->count('id');
        if($pass_count!=0){
            $return=intval(($pass_count/$all_count)*100);
        }else{
            $return = 0 ;
        }
        return $return;
    }
    public function signUser($user_id,$jointime)
    {
        $UserScoreLog = new UserScoreLog();
        $diffInSeconds=time()-$jointime;
        $days = floor($diffInSeconds / (60 * 60 * 24))+1;
        $where['user_id']=$user_id;
        $sign_count=$UserScoreLog->where($where)->count();
        if($sign_count!=0){
            $return=intval(($sign_count/$days)*100);
        }else{
            $return = 0 ;
        }
        return $return;
    }

    public function daySign()
    {
        $UserSign = new UserSign();
        $where['createtime']=['>',strtotime(date('Y-m-d 00:00:00',time()))];
        $count=$UserSign->where($where)->count();
        $this->success('',$count);
    }

    public function userInfo()
    {
        $param = $this->request->param();
        $UserModel = new UserModel();
        $Category = new Category();
        $find=$UserModel->where(['mobile'=>$param['mobile']])->field('id,nickname,avatar,score,identity')->find();
        if(empty($find)){
            $this->error('','用户不存在');
        }
        $find['identity']=$Category->where(['id'=>$find['identity']])->value('name');
        $this->success('',$find);
    }
    public function adjustScore(){
        $param = $this->request->param();
        $user= $this->auth;
        $AdjustScoreLog = new AdjustScoreLog();
        $data['user_id']=$user->id;
        $data['adjust_id']=$param['adjust_id'];
        $data['status']=$param['status'];
        $data['num']=$param['num'];
        $data['notes']=$param['notes'];
        $data['file_images']=$param['file_images'];
        $data['createtime']=time();
        $AdjustScoreLog->save($data);

        if($param['status']==1){
            $type=1;
        }else{
            $type=0;
        }
        $this->score_change($param['adjust_id'],$param['num'],$type,$user->nickname."修改积分",0);
        $this->success('');
    }
    public function MissExam()
    {
        $where['createtime']=['<',strtotime(date('Y-m-d 12:59:59',time()))];
        $list=db('user_miss_exam')->where($where)->paginate();
        $this->success('',$list);
    }
}
